﻿using AutoMail.Log.QuartzLog;
using AutoMail.QuartzJobs.Jobs.Utils;
using AutoMail.Tools.EmailTools;
using Quartz;
using Quartz.Job;
using System.Net;
using System.Net.Mail;

namespace AutoMail.QuartzJobs.Jobs
{
    [PersistJobDataAfterExecution]
    public class BirthdayMailSendJob : SendMailJob
    {
        public const string PropertyStaffName = "curStaffName";
        public static readonly string PropertyEnableSsl = "EnableSsl";
        public static readonly string JobGroup = "BirthdayMailJob";
        public static readonly string JobBuildDescription = "Sending a birthday email every year to a staff";

        public BirthdayMailSendJob()
        {

        }

        public override async Task Execute(IJobExecutionContext context)
        {
            QuartzLogger.LogUserInfo("[BirthdayMailSendJob] Execute Start......");
            string staffName = context.MergedJobDataMap.GetString(PropertyStaffName);
            var mailBody = context.MergedJobDataMap.GetString(PropertyMessage);

            // If the value is a file path, then it must be the Email template file.
            var isHtmlFile = File.Exists(mailBody);
            MailInfo mailInfo = new MailInfo
            {
                MailMessage = new MailMessage(context.MergedJobDataMap.GetString(PropertySender)
                    , context.MergedJobDataMap.GetString(PropertyRecipient))
                {
                    Subject = EmailSubject.Birthday.GetString(staffName: staffName),
                    Body = isHtmlFile ? GetMailTemplate(mailBody, staffName) : mailBody,
                    IsBodyHtml = isHtmlFile
                },
                SmtpHost = context.MergedJobDataMap.GetString(PropertySmtpHost),
                SmtpPort = context.MergedJobDataMap.GetInt(PropertySmtpPort),
                SmtpUserName = context.MergedJobDataMap.GetString(PropertyUsername),
                SmtpPassword = context.MergedJobDataMap.GetString(PropertyPassword),
            };

            QuartzLogger.LogUserInfo("[BirthdayMailSendJob] Finished Mail Send preparation......");

            await Send(mailInfo, context.MergedJobDataMap.GetBoolean(PropertyEnableSsl));
            QuartzLogger.LogUserInfo("[BirthdayMailSendJob] Finished Mail Sending......");
        }

        private string GetMailTemplate(string mailTemplatePath, string staffName)
        {
            using (StreamReader rd = new StreamReader(mailTemplatePath))
            {
                return rd.ReadToEnd().Replace("{#base64PicBirthdayMail#}", PictureBase64Generating.GetBirthDayMailTemplatePictureBase64String(staffName, DateTime.Now.ToShortDateString()) ?? "");
            }
        }

        protected new async Task Send(MailInfo mailInfo, bool enableSsl = true)
        {
            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

            SmtpClient client = new SmtpClient(mailInfo.SmtpHost, mailInfo.SmtpPort ?? 0);

            client.Credentials = new NetworkCredential(mailInfo.SmtpUserName, mailInfo.SmtpPassword);
            client.EnableSsl = enableSsl;
            await client.SendMailAsync(mailInfo.MailMessage);

            QuartzLogger.LogUserInfo("[BirthdayMailSendJob] ------****** sending Sucessfully ******------");
        }
    }
}
